package com.itheima.controller;

import com.itheima.model.Customer;
import com.itheima.service.DiscoutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
public class DiscoutController {

    @Autowired
    private DiscoutService discoutService;

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
    @GetMapping("/getCustomer")
    public Customer geyCustomer(@RequestParam String idCard){
        Customer customer = discoutService.getCustomer(idCard);
        if (customer==null){
            return new Customer();
        }
        return customer;
    }
    @GetMapping("/getVipLevel")
    public String getVipLevel(@RequestParam String idCard){
        Customer customer = discoutService.getCustomer(idCard);
        if (customer.getVipType()==null){
            return "非会员用户";
        }else {
            return customer.getVipType();
        }
    }
    @GetMapping("/discount")
    public String getDiscount(@RequestParam String itemName,
                              @RequestParam BigDecimal price,
                              @RequestParam String vipLevel){
        try {
            BigDecimal currentPrice = discoutService.discount(vipLevel,itemName,price);
            return currentPrice.toString();
        }catch(Exception e ){
            e.printStackTrace();
            return "Error";
        }
    }


}
